package StudyCode.summary.BufferAndChannel;

import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.EnumSet;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CopyFileUsingFileChannelTransferFrom {
	public static void main(String[] args) {
		Path copy_from = Paths.get("E:\\Copy", "1.txt");
		Path copy_to = Paths.get("E:/Copy/2.txt");
		System.out.println("使用 FileChannel.transferFrom 方法复制文件 ...");
		try (FileChannel fileChannel_from = (FileChannel.open(copy_from,
				EnumSet.of(StandardOpenOption.READ)));
		     FileChannel fileChannel_to = (FileChannel
				     .open(copy_to, EnumSet.of(StandardOpenOption.CREATE_NEW,
						     StandardOpenOption.WRITE)))) {

			fileChannel_to.transferFrom(fileChannel_from, 0L, (int) fileChannel_from.size());
			System.out.println("使用 FileChannel.transferFrom 方法复制文件成功");
		} catch (IOException ex) {
			System.err.println(ex);
		}
	}
}
